//
//  M4UserDataManager.swift
//  M4
//
//  Created by YuChuang on 2024/8/7.
//

import UIKit

class M4UserDataManager: NSObject {

    static let m4UserShare = M4UserDataManager()
    
    var M4Itemcode = "SPO"
    
    var M4Version: Int {
        get {
            let version = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
            let result = version.replacingOccurrences(of: ".", with: "")
            return Int(result) ?? 1
        }
    }
    
    var M4BundleID = Bundle.main.infoDictionary!["CFBundleIdentifier"] as! String
    
    var M4ProtocolAgree: Bool {
        get {
            return UserDefaults.standard.bool(forKey: "M4ProtocolAgree")
        }
        set {
            let userDefaults = UserDefaults.standard
            userDefaults.set(newValue, forKey: "M4ProtocolAgree")
            userDefaults.synchronize()
        }
    }
    
    var M4UserMobile: String {
        get {
            return UserDefaults.standard.string(forKey: "M4UserMobile") ?? ""
        }
        set {
            let userDefaults = UserDefaults.standard
            userDefaults.set(newValue, forKey: "M4UserMobile")
            userDefaults.synchronize()
        }
    }
    
    var M4AuditAccount: Int {
        get {
            return UserDefaults.standard.integer(forKey: "M4AuditAccount")
        }
        set {
            let userDefaults = UserDefaults.standard
            userDefaults.set(newValue, forKey: "M4AuditAccount")
            userDefaults.synchronize()
        }
    }
    
    var M4Token: String {
        get {
            return UserDefaults.standard.string(forKey: "M4Token") ?? ""
        }
        set {
            let userDefaults = UserDefaults.standard
            userDefaults.set(newValue, forKey: "M4Token")
            userDefaults.synchronize()
        }
    }
    
    var M4appUserId: String {
        get {
            return UserDefaults.standard.string(forKey: "M4appUserId") ?? ""
        }
        set {
            let userDefaults = UserDefaults.standard
            userDefaults.set(newValue, forKey: "M4appUserId")
            userDefaults.synchronize()
        }
    }
    
    var M4userName: String {
        get {
            return UserDefaults.standard.string(forKey: "M4userName") ?? ""
        }
        set {
            let userDefaults = UserDefaults.standard
            userDefaults.set(newValue, forKey: "M4userName")
            userDefaults.synchronize()
        }
    }
    
    var M4Latitude: String = "0"
    
    var M4Longtitude: String = "0"
    
    var M4ContactData: [[String:String]] {
        get {
            return UserDefaults.standard.object(forKey: "M4ContactData") as? [[String : String]] ?? []
        }
        set {
            let userDefaults = UserDefaults.standard
            userDefaults.set(newValue, forKey: "M4ContactData")
            userDefaults.synchronize()
        }
    }
    
    var M4ProductCode: String = "SPAO"
    
    
    class var M4HasShowUploadPage: Bool {
        get {
//            return UserDefaults.standard.bool(forKey: "M4HasShowUploadPage")
            return false
        }
        set {
            UserDefaults.standard.set(newValue, forKey: "M4HasShowUploadPage")
            UserDefaults.standard.synchronize()
        }
    }
    
    
    var adjustAdid: String {
        set {
            if newValue.count == 0 { return }
            UserDefaults.standard.set(newValue, forKey: "adjustAdid")
            UserDefaults.standard.synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "adjustAdid") ?? ""
        }
    }
    var adjustIdfa: String {
        set {
            if newValue.count == 0 { return }
            UserDefaults.standard.set(newValue, forKey: "adjustIdfa")
            UserDefaults.standard.synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "adjustIdfa") ?? ""
        }
    }
    var adjustIdfv: String {
        set {
            if newValue.count == 0 { return }
            UserDefaults.standard.set(newValue, forKey: "adjustIdfv")
            UserDefaults.standard.synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "adjustIdfv") ?? ""
        }
    }
}
